import Vue from 'vue'
import VueRouter from 'vue-router'
import Product from '../components/product.vue'
import ProductDetial from '../components/productdetial.vue'
import List from '../components/list.vue'
import Cart from '../components/cart.vue'

Vue.use(VueRouter)

//路由信息配置
const Routes = [{
    path: '/list',   
    component: List,
    meta:{title:'商品列表'}
  },
  {
    path: '/product/:id',   
    component: ProductDetial,
    meta:{title:'商品详情'}
  },
  {
    path: '/cart',   
    component: Cart,
    meta:{title:'购物车'}
  },
  {
    path: '*',
    redirect: '/list'
  }


]



//全局路由模式设置
const RouteConfig = {
  mode: 'history',
  routes: Routes
}

const router = new VueRouter(RouteConfig);

//全局路由进入前守卫
router.beforeEach((to, from, next) => {
  //修改页面名称
  window.document.title = to.meta.title;
  next();
});

//路由进入后守卫
router.afterEach((to, from, next) => {
  //滑动条重置
  window.scrollTo(0, 0);
});

export default router
